/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.activity.subcategory;


import android.annotation.SuppressLint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.dataoke.shoppingguide.app773026.R;
import com.dataoke.shoppingguide.app773026.activity.search.SearchActivity;
import com.dataoke.shoppingguide.app773026.base.fragments.BasePagerFragment;
import com.dataoke.shoppingguide.app773026.bean.CategoryBean;
import com.dataoke.shoppingguide.app773026.bean.DataokeGetCategorySubDataBean;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;

/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class CategoryListFragment extends BasePagerFragment implements
        View.OnClickListener {
    private int mCurrentItem;
    private ArrayList<DataokeGetCategorySubDataBean> subDataBeans;
    private int index;

    public static CategoryListFragment newInstance() {
        return new CategoryListFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_category_list;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        subDataBeans = (ArrayList)getActivity().getIntent().getParcelableArrayListExtra("data");
        index = getActivity().getIntent().getIntExtra("index", 0);
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setTitleText(false, mCurrentItem);
                setTitleText(true, position);
                mCurrentItem = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mViewPager.setCurrentItem(index);
        if(index == 0) {
            setTitleText(true, 0);
        }
    }
    @Override
    protected void initData() {

    }

    private void setTitleText(boolean isSelected, int position) {
        TabLayout.Tab tab = mTabLayout.getTabAt(position);
        if (tab == null) return;
        View view = tab.getCustomView();
        if (view == null) return;
        TextView textView = view.findViewById(R.id.tv_title);

        if(isSelected) {
            textView.setTypeface(Typeface.DEFAULT_BOLD);
            textView.setTextColor(getResources().getColor(R.color.c333));
        } else {
            textView.setTypeface(Typeface.DEFAULT);
            textView.setTextColor(getResources().getColor(R.color.gray));
        }

    }

    @Override
    protected void setupTabView() {
        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
                if (tab.getCustomView() != null) {
                    View tabView = (View) tab.getCustomView().getParent();
                    tabView.setTag(i);
                    tabView.setOnClickListener(v -> {

                    });
                }
            }
        }
    }

    @SuppressLint("InflateParams")
    private View getTabView(int i) {
        View view = mInflater.inflate(R.layout.tab_synthesize, null);
        TextView tv_title = view.findViewById(R.id.tv_title);
        tv_title.setText(mAdapter.getPageTitle(i));
        return view;
    }

    @OnClick({R.id.iv_search, R.id.iv_back})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_search:
                SearchActivity.show(getContext());
                break;
            case R.id.iv_back:
                getActivity().onBackPressed();
                break;
        }
    }

    @Override
    protected List<Fragment> getFragments() {
        List<Fragment> fragments = new ArrayList<>(subDataBeans.size());

        for(DataokeGetCategorySubDataBean subDataBean : subDataBeans) {
            fragments.add(HomeDataokeFragment.newInstance(subDataBean.getSubcid() + "", "39.9", true));
        }
        return fragments;
    }

    @Override
    protected List<CategoryBean> getBeans() {

        List<CategoryBean> beans = new ArrayList<>(subDataBeans.size());
        for(DataokeGetCategorySubDataBean subDataBean : subDataBeans) {
            beans.add(new CategoryBean(subDataBean.getSubcname(), "-1"));
        }
        return beans;
    }
}
